package com.jic.order.impl;

import java.lang.Long;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.order.entity.OrderDeliveryRelease;
import com.jic.order.manage.request.OrderDeliveryReleaseRequest;
import com.jic.order.mapper.OrderDeliveryReleaseMapper;
import com.jic.order.service.OrderDeliveryReleaseService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;

import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 交货单发布管理
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-05-29 11:42:19
 */
@Service
public class OrderDeliveryReleaseServiceImpl implements OrderDeliveryReleaseService {

    @Autowired
    private OrderDeliveryReleaseMapper orderDeliveryReleaseMapper;

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Override
    public RestResult saveOrderDeliveryRelease(OrderDeliveryReleaseRequest orderDeliveryReleaseRequest) {
        if (CollectionUtils.isEmpty(orderDeliveryReleaseRequest.getOrderDeliveryReleaseRequestList())) {
            return RestResult.error("9999", "请勾选要抛单的产品");
        } else {
            String userStr = redisClientTemplate.get(orderDeliveryReleaseRequest.getToken());
            Map sysUser = JSONArray.parseObject(userStr, Map.class);
            //判断当前订单号是否已经发布
            OrderDeliveryRelease orderDeliveryRelease1 = new OrderDeliveryRelease();
            orderDeliveryRelease1.setOrderNo(orderDeliveryReleaseRequest.getOrderDeliveryReleaseRequestList().get(0).getOrderNo());
            List<OrderDeliveryRelease> orderDeliveryReleaseList = orderDeliveryReleaseMapper.query(orderDeliveryRelease1);
            if (CollectionUtils.isEmpty(orderDeliveryReleaseList)) {
                orderDeliveryReleaseRequest.getOrderDeliveryReleaseRequestList().forEach(item -> {
                    OrderDeliveryRelease orderDeliveryRelease = new OrderDeliveryRelease();
                    BeanUtils.copyProperties(item, orderDeliveryRelease);
                    orderDeliveryRelease.setMerchantId(Long.valueOf(sysUser.get("merchantId").toString()));
                    orderDeliveryReleaseMapper.insert(orderDeliveryRelease);
                });
            } else {
               //先删除
                orderDeliveryReleaseMapper.deleteByOrderNo(orderDeliveryReleaseRequest.getOrderDeliveryReleaseRequestList().get(0).getOrderNo());
                //重新插入
                orderDeliveryReleaseRequest.getOrderDeliveryReleaseRequestList().forEach(item -> {
                    OrderDeliveryRelease orderDeliveryRelease = new OrderDeliveryRelease();
                    BeanUtils.copyProperties(item, orderDeliveryRelease);
                    orderDeliveryRelease.setMerchantId(Long.valueOf(sysUser.get("merchantId").toString()));
                    orderDeliveryReleaseMapper.insert(orderDeliveryRelease);
                });
            }

            return RestResult.success(true);
        }
    }

    @Override
    public long delete(Long id) {
        return orderDeliveryReleaseMapper.delete(id);
    }

    @Override
    public long update(OrderDeliveryRelease orderDeliveryRelease) {
        return orderDeliveryReleaseMapper.update(orderDeliveryRelease);
    }

    @Override
    public OrderDeliveryRelease queryByPrimaryKey(Long id) {
        return orderDeliveryReleaseMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<OrderDeliveryRelease> query(OrderDeliveryRelease orderDeliveryRelease) {
        return orderDeliveryReleaseMapper.query(orderDeliveryRelease);
    }

    @Override
    public PageResult<OrderDeliveryRelease> queryPage(OrderDeliveryRelease orderDeliveryRelease, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderDeliveryRelease> list = orderDeliveryReleaseMapper.queryPage(startIndex, pageQuery.getLimit(), orderDeliveryRelease);
        long count = orderDeliveryReleaseMapper.queryPageCount(orderDeliveryRelease);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}